import java.sql.ResultSet;

import oracle.jdbc.aq.AQAgent;
import oracle.jdbc.driver.OracleConnection;


public class DBMS_PROPAGATION_ADMUtil extends OracleAQ {

	public DBMS_PROPAGATION_ADMUtil() {}
	
	public DBMS_PROPAGATION_ADMUtil( String user, String password, String host, String port, String sid) {
		super( user, password, host, port, sid);
	
	}
	
	public DBMS_PROPAGATION_ADMUtil( String user, String password, String url) {
		super( user, password, url);
	
	}
	
	/**
	 * This procedure starts a propagation.
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int start_Propagation( String propagationName) {
		
		try {
			this.connect();
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.START_PROPAGATION(?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.execute();
			logger.info( propagationName + " propagation start.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	
	}
	
	/**
	 * This procedure starts a propagation.
	 * @param conn
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int start_Propagation( OracleConnection conn, String propagationName) {
		
		try {
			this.conn = conn;
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.START_PROPAGATION(?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.execute();
			logger.info( propagationName + " propagation start.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		}
		
		return 0;
	
	}
	
	/**
	 * This procedure stops a propagation.
	 * force(Default true)
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int stop_Propagation( String propagationName) {
		
		try {
			this.connect();
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.STOP_PROPAGATION( ?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.execute();
			logger.info( propagationName + " propagation stopped.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	}
	
	/**
	 * This procedure stops a propagation.
	 * @param conn
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int stop_Propagation( OracleConnection conn, String propagationName) {
		
		try {
			this.conn = conn;
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.STOP_PROPAGATION( ?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.execute();
			logger.info( propagationName + " propagation stopped.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	}
	
	public int create_Propagation( String propagationName, String source_QU) {
		
		try {
			this.drop_Propagation(propagationName, "FALSE");
						
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	
	}
	
	
	/**
	 * This procedure drops a propagation and deletes all messages for the destination queue in the source queue.
	 * This procedure also removes the schedule for propagation from the source queue to the destination queue.
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int drop_Propagation( String propagationName) {
		
		try {
			this.drop_Propagation(propagationName, "FALSE");
						
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	
	}
	
	/**
	 * This procedure drops a propagation and deletes all messages for the destination queue in the source queue.
	 * This procedure also removes the schedule for propagation from the source queue to the destination queue.
	 * @param conn
	 * @param propagationName
	 * @return success : 0, fail : 1
	 */
	public int drop_Propagation( OracleConnection conn, String propagationName) {
		
		try {
			this.drop_Propagation( conn, propagationName, "FALSE");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		}
		
		return 0;
	
	}
	
	/**
	 * This procedure drops a propagation and deletes all messages for the destination queue in the source queue.
	 * This procedure also removes the schedule for propagation from the source queue to the destination queue.
	 * @param propagationName
	 * @param drop_Unused_Rule_Sets
	 * @return success : 0, fail : 1
	 */
	public int drop_Propagation( String propagationName, String drop_Unused_Rule_Sets) {
		
		try {
			this.connect();
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.DROP_PROPAGATION(?, ?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.setString( 2, drop_Unused_Rule_Sets);
			this.cstmt.execute();
			logger.info( propagationName + " propagation dropped.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		} finally {
			this.disconnect();
		
		}
		
		return 0;
	
	}
	
	/**
	 * This procedure drops a propagation and deletes all messages for the destination queue in the source queue.
	 * This procedure also removes the schedule for propagation from the source queue to the destination queue.
	 * @param conn
	 * @param propagationName
	 * @param drop_Unused_Rule_Sets
	 * @return success : 0, fail : 1
	 */
	public int drop_Propagation( OracleConnection conn, String propagationName, String drop_Unused_Rule_Sets) {
		
		try {
			this.conn = conn;
			this.cstmt = this.conn.prepareCall("{call DBMS_PROPAGATION_ADM.DROP_PROPAGATION(?, ?)}");
			this.cstmt.setString( 1, propagationName);
			this.cstmt.setString( 2, drop_Unused_Rule_Sets);
			this.cstmt.execute();
			logger.info( propagationName + " propagation dropped.");
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		}
		
		return 0;
	
	}
	
	public int getSubscriber( OracleConnection conn, String queue_Name) {
		
		try {
			this.conn = conn;
//			this.cstmt = this.conn.prepareCall("{call DBMS_AQADM.QUEUE_SUBSCRIBERS(?)}");
			this.cstmt = this.conn.prepareCall("select DBMS_AQADM.QUEUE_SUBSCRIBERS(?) from dual");
			this.cstmt.setString( 1, queue_Name);
			ResultSet rs= null;
			rs = this.cstmt.executeQuery();
			
			while( rs.next()) {
				AQAgent ag = ( AQAgent)rs.getObject( 1);
				System.out.println( "getName = " + ag.getName());
				
			}
			
		} catch( Exception e) {
			logger.error( e.getMessage());
			return 1;
			
		}
		
		return 0;
	
	}
	
	public static void main( String[] argv) {
		DBMS_PROPAGATION_ADMUtil test = new DBMS_PROPAGATION_ADMUtil( "strmadmin", "streams", "192.168.0.98", "1521", "bizteco");
//		DBMS_PROPAGATION_ADMUtil test = new DBMS_PROPAGATION_ADMUtil( "strmadmin", "streams", "192.168.0.198", "1521", "bizteco");
//		DBMS_PROPAGATION_ADMUtil test = new DBMS_PROPAGATION_ADMUtil( "strmadmin", "strmadmin", "192.168.0.92", "1521", "bizteco");
		OracleConnection conn = test.connect();
//		test.start_Propagation( "S_TO_T");
		test.stop_Propagation( "S_TO_T");
		
//		test.getSubscriber( conn, "strmadmin.Q_CAPTURE");
		
		test.disconnect();
	}
	
}
